package org.uned.agonzalo16.bitacora.dao;

import static org.uned.agonzalo16.bitacora.dao.OfyFactory.ofy;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.uned.agonzalo16.bitacora.domain.Article;
import org.uned.agonzalo16.bitacora.domain.Comment;
import org.uned.agonzalo16.bitacora.domain.User;

@Repository
public class CommentDao {

	public List<Comment> findAll() {
		return ofy().load().type(Comment.class).list();
	}

	public List<Comment> findByArticle(Article article) {
		return ofy().load().type(Comment.class).ancestor(article).order("creationDate").list();
	}

	public int countByArticle(Article article) {
		return ofy().load().type(Comment.class).ancestor(article).count();
	}

	public List<Comment> findByUser(User user) {
		return ofy().load().type(Comment.class).filter("user", user).list();
	}

	public Comment get(Article article, Long id) {
		return ofy().load().type(Comment.class).parent(article).id(id).now();
	}

	public Comment merge(Comment comment) {
		ofy().save().entity(comment).now();
		return comment;
	}

	public void delete(Article article, Long id) {
		ofy().delete().type(Comment.class).parent(article).id(id).now();
	}
}
